package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.OrderDetail;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.orderform.InsertOrderDetailForm;
import com.example.wechattakeaway.mapper.OrderDetailMapper;
import com.example.wechattakeaway.service.OrderDetailService;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Repository
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public ResultVO InsertOrderDetail(InsertOrderDetailForm insertOrderDetailForm) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(insertOrderDetailForm.getOrderId());
        orderDetail.setDishId(insertOrderDetailForm.getDishId());
        orderDetail.setDishNum(insertOrderDetailForm.getDishNum());

        try{
            orderDetailMapper.InsertOrderDetail(orderDetail);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(orderDetail);
    }

    @Override
    public ResultVO DeleteOrderDetail(Integer orderDetailId) {
        try{
            orderDetailMapper.DeleteOrderDetail(orderDetailId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO SelectOrderDetailById(Integer orderDetailId) {
        OrderDetail orderDetail = new OrderDetail();
        try{
            orderDetail = orderDetailMapper.SelectOrderDetailById(orderDetailId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(orderDetail);
    }

    @Override
    public ResultVO SelectOrderDetailByOrder(Integer pageNum,Integer orderId) {
        PageHelper.startPage(pageNum,5);

        List<OrderDetail> orderDetailList = new ArrayList<>();
        try{
            orderDetailList = orderDetailMapper.SelectOrderDetailByOrder(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(orderDetailList);
    }

}
